package com.hxkcloud.patient.aerotel.domain.health;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;


import com.hxkcloud.patient.aerotel.database.ECGTable;
import com.hxkcloud.patient.aerotel.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daipengfei on 16/3/27.
 */
public class ECGManager {

    public static Map<Long, AerotelECGPdu> ecgsmap = new HashMap<>();

    public static AerotelECGPdu getFromCache(Long id) {
        return ecgsmap.get(id);
    }

    public static List<AerotelECGPdu> getECGs(Context context, String device, String date) {
        String selection = "";
        if (StringUtil.isNotNullOrEmpty(device)) {
            selection += " device='" + device + "' ";
        }
        if (StringUtil.isNotNullOrEmpty(date)) {
            if (StringUtil.isNotNullOrEmpty(selection)) {
                selection += " AND created_date LIKE '" + date + "%' ";
            } else {
                selection += " created_date LIKE '" + date + "%' ";
            }
        }
        List<AerotelECGPdu> ecgs = new ArrayList<>();
        Cursor c = context.getContentResolver().query(ECGTable.CONTENT_URI, null, selection, null, ECGTable.DEFAULT_ORDERBY);
        c.moveToFirst();
        do {
            if (!c.isAfterLast()) {
                AerotelECGPdu ecg = new AerotelECGPdu();
                ecg.id = c.getLong(c.getColumnIndex(ECGTable._ID));
                ecg.devicename = c.getString(c.getColumnIndex(ECGTable.DEVICE));
                ecg.devicesn = c.getString(c.getColumnIndex(ECGTable.DEVICE_SN));
                ecg.devicebtmac = c.getString(c.getColumnIndex(ECGTable.DEVICE_BTMAC));
                ecg.createdDate = c.getString(c.getColumnIndex(ECGTable.CREATED_DATE));
                ecg.file = c.getString(c.getColumnIndex(ECGTable.ECG_FILE));
                ecgs.add(ecg);
                ecgsmap.put(ecg.id, ecg);
            }
        } while (c.moveToNext());
        return ecgs;
    }



    public static int removeECG(Context context, long id) {
        String selection = "";
        if (id!=0) {
            selection += BaseColumns._ID+" = "+id ;
        }
        return context.getContentResolver().delete(ECGTable.CONTENT_URI, selection, null);
    }




    public static List<AerotelECGPdu> getECGs(Context context, String date) {
        String selection = "";

        if (StringUtil.isNotNullOrEmpty(date)) {
                selection += " created_date LIKE '" + date + "%' ";
        }
        List<AerotelECGPdu> ecgs = new ArrayList<>();
        Cursor c = context.getContentResolver().query(ECGTable.CONTENT_URI, null, selection, null, ECGTable.DEFAULT_ORDERBY);
        c.moveToFirst();
        do {
            if (!c.isAfterLast()) {
                AerotelECGPdu ecg = new AerotelECGPdu();
                ecg.id = c.getLong(c.getColumnIndex(ECGTable._ID));
                ecg.devicename = c.getString(c.getColumnIndex(ECGTable.DEVICE));
                ecg.devicesn = c.getString(c.getColumnIndex(ECGTable.DEVICE_SN));
                ecg.devicebtmac = c.getString(c.getColumnIndex(ECGTable.DEVICE_BTMAC));
                ecg.createdDate = c.getString(c.getColumnIndex(ECGTable.CREATED_DATE));
                ecg.file = c.getString(c.getColumnIndex(ECGTable.ECG_FILE));
                ecg.state = c.getShort(c.getColumnIndex(ECGTable.UPLOAD_STATE));
                ecgs.add(ecg);
                ecgsmap.put(ecg.id, ecg);
            }
        } while (c.moveToNext());
        return ecgs;
    }


    public static AerotelECGPdu getECG(Context context, long id) {
        Uri uri = Uri.withAppendedPath(ECGTable.CONTENT_URI, String.valueOf(id));
        Cursor c = context.getContentResolver().query(uri, null, null, null, ECGTable.DEFAULT_ORDERBY);
        c.moveToFirst();
        if (!c.isAfterLast()) {
            AerotelECGPdu ecg = new AerotelECGPdu();
            ecg.id = c.getLong(c.getColumnIndex(ECGTable._ID));
            ecg.devicename = c.getString(c.getColumnIndex(ECGTable.DEVICE));
            ecg.devicesn = c.getString(c.getColumnIndex(ECGTable.DEVICE_SN));
            ecg.devicebtmac = c.getString(c.getColumnIndex(ECGTable.DEVICE_BTMAC));
            ecg.createdDate = c.getString(c.getColumnIndex(ECGTable.CREATED_DATE));
            ecg.file = c.getString(c.getColumnIndex(ECGTable.ECG_FILE));
            ecgsmap.put(ecg.id, ecg);
            return ecg;
        }
        return null;
    }

    public static Uri create(Context context, AerotelECGPdu ecg) {
        ContentValues cv = ECGTable.getContentValues(ecg);
        Uri uri = context.getContentResolver().insert(ECGTable.CONTENT_URI, cv);
        return uri;
    }

    public static void delete(Context context, String date) {
        Uri uri = Uri.withAppendedPath(ECGTable.CONTENT_URI, String.valueOf(date));
        ContentValues cv = new ContentValues();
        context.getContentResolver().delete(uri, "id=?", new String[]{String.valueOf(date)});

    }


    public static int markasupload(Context context, long id) {
        Uri uri = Uri.withAppendedPath(ECGTable.CONTENT_URI, String.valueOf(id));
        ContentValues cv = new ContentValues();
        cv.put("state", 1);
        int i = context.getContentResolver().update(uri, cv, null, null);
        return i;
    }

    public static int markasuploadfailed(Context context, long id) {
        Uri uri = Uri.withAppendedPath(ECGTable.CONTENT_URI, String.valueOf(id));
        ContentValues cv = new ContentValues();
        cv.put("state", -1);
        int i = context.getContentResolver().update(uri, cv, null, null);
        return i;
    }
}
